home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvitops / psfont.c < prev    next >
C/C++ Source or Header  |  1991-01-25  |  3KB  |  146 lines

  1. static char rcsid[] = "$Header: /usr/jjc/dvitops/RCS/psfont.c,v 1.2 89/02/20 14:24:06 jjc Rel $";
  2.  
  3. #include "util.h"
  4.  
  5. #define LINE_MAX 512
  6. char *program_name = "psfont";
  7. static char *ws = " \n\r\t";    /* white space */
  8. static char *psfonts;
  9.  
  10. #ifdef PROTO
  11. int strprefix(char *s1, char *s2);
  12. void fcopy(FILE *in, FILE *out);
  13. void process_fonts(void);
  14. #endif
  15.  
  16. /* is s1 a prefix of s2? */
  17. int strprefix(s1, s2)
  18. char *s1, *s2;
  19. {
  20.     while (*s1++ == *s2++)
  21.         ;
  22.     return s1[-1] == '\0';
  23. }
  24.  
  25. /* copy infp to outfp */
  26. void fcopy(infp, outfp)
  27. FILE *infp, *outfp;
  28. {
  29.     int c;
  30.     while ((c = getc(infp)) != EOF)
  31.         putc(c, outfp);
  32. }
  33.     
  34. struct string_list {
  35.     struct string_list *next;
  36.     char *s;
  37.     char *t;
  38. };
  39. struct string_list *document_fonts = NULL;
  40.  
  41. /* map PostScript names to file names */
  42.  
  43. void process_fonts()
  44. {
  45.     char buf[LINE_MAX];
  46.     FILE *fp;
  47.     fp = xfopen("psfonts.map", FALSE, psfonts, NULL);
  48.     if (fp == 0)
  49.         return;
  50.     while (fgets(buf, LINE_MAX, fp) != 0) {
  51.         char *s, *t;
  52.         s = strchr(buf, '%');
  53.         if (s != NULL)
  54.             *s = '\0';
  55.         s = strtok(buf, ws);
  56.         t = strtok(NULL, ws);
  57.         if (s != 0 && t != 0) {
  58.             struct string_list *q;
  59.             for (q = document_fonts; q != NULL; q = q->next)
  60.                 if (strcmp(q->s, s) == 0) {
  61.                     q->t = malloc(strlen(t) + 1);
  62.                     if (q->t == NULL)
  63.                         out_of_memory();
  64.                     strcpy(q->t, t);
  65.                 }
  66.         }
  67.     }
  68.     fclose(fp);
  69. }
  70.  
  71.  
  72. int main(argc, argv)
  73. int argc;
  74. char **argv;
  75. {
  76.     int flag = FALSE;
  77.     int cont = FALSE;
  78.     char *ptr;
  79.     char buf[LINE_MAX];
  80.     psfonts = getenv("PSFONTS");
  81.     if (psfonts == NULL)
  82.         psfonts = PSFONTS;
  83.     if (fgets(buf, LINE_MAX, stdin) == 0)
  84.         exit(0);
  85.     fputs(buf, stdout);
  86.     if (strprefix("%!PS-Adobe-", buf))
  87.         while (fgets(buf, LINE_MAX, stdin) != 0) {
  88.             if (!strprefix("%%", buf)) {
  89.                 flag = TRUE;
  90.                 fputs("%%EndComments\n", stdout);
  91.                 break;
  92.             }
  93.             fputs(buf, stdout);
  94.             if (strprefix("%%EndComments", buf))
  95.                 break;
  96.             if (document_fonts == NULL && strprefix("%%DocumentFonts:", buf))
  97.                 ptr = strchr(buf, ':') + 1;
  98.             else if (cont && strprefix("%%+", buf))
  99.                 ptr = buf + 3;
  100.             else
  101.                 ptr = NULL;
  102.             if (ptr != NULL) {
  103.                 for (ptr = strtok(ptr, ws);
  104.                      ptr != 0;
  105.                      ptr = strtok(NULL, ws)) {
  106.                     struct string_list *p;
  107.                     p = (struct string_list *)
  108.                         malloc(sizeof(struct string_list));
  109.                     if (p == 0)
  110.                         out_of_memory();
  111.                     p->s = malloc(strlen(ptr) + 1);
  112.                     p->t = NULL;
  113.                     if (p->s == 0)
  114.                         out_of_memory();
  115.                     strcpy(p->s, ptr);
  116.                     p->next = document_fonts;
  117.                     document_fonts = p;
  118.                 }
  119.                 cont = TRUE;
  120.             }
  121.             else
  122.                 cont = FALSE;
  123.         }
  124.     process_fonts();
  125.     while (document_fonts != 0) {
  126.         FILE *fp;
  127.         char *s;
  128.         if (document_fonts->t != NULL)
  129.             s = document_fonts->t;
  130.         else
  131.             s = document_fonts->s;
  132.         fp = xfopen(s, FALSE, psfonts, NULL);
  133.         if (fp != 0) {
  134.             printf("%%%%BeginFont: %s\n", document_fonts->s);
  135.             fcopy(fp, stdout);
  136.             fclose(fp);
  137.             printf("%%%%EndFont\n");
  138.         }
  139.         document_fonts = document_fonts->next;
  140.     }
  141.     if (flag)
  142.         fputs(buf, stdout);
  143.     fcopy(stdin, stdout);
  144.     exit(history);
  145. }
  146.